package com.sylli.oeuf.server.db.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.MapBlockTO;

public class MySQLMapBlock extends MySQLBasedProvider<MapBlockTO> {

	public MySQLMapBlock() {
		interactTableName = "map_block";
	}
	
//	public MySQLMapBlock(DatabaseCache<MapBlockTO> cache) {
//		super(cache);
//		interactTableName = "map_block";
//	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(MapBlockTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (block_id, map_id, flag, param, terrain_type)"
					+ " VALUES (?,?,?,?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, obj.getBlockId());
			ps.setInt(2, obj.getMapId());
			ps.setInt(3, obj.getFlag());
			ps.setString(4, obj.getParam());
			ps.setInt(5, obj.getTerrainType());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(MapBlockTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET flag=?,param=?,terrain_type=? WHERE block_id = ? AND map_id=?;");
			ps.setInt(1, obj.getFlag());
			ps.setString(2, obj.getParam());
			ps.setInt(3, obj.getTerrainType());
			ps.setInt(4, obj.getBlockId());
			ps.setInt(5, obj.getMapId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected MapBlockTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			MapBlockTO sto = new MapBlockTO();
			rs.first();
			sto.setBlockId(rs.getInt(1));
			sto.setMapId(rs.getInt(2));
			sto.setFlag(rs.getInt(3));
			sto.setParam(rs.getString(4));
			sto.setTerrainType(rs.getInt(5));
			return sto;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<MapBlockTO> convertResultSetToObjects(ResultSet rs)
			throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<MapBlockTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}
}
